import Home from './common/game1.js'; // 导入首页
import Goid from '../page/common/gold.js'; // 导入金币
import ClickIndex from './click/index.js'; // 导入点击事件

/**
 * 游戏主函数
 */
export default class Main {

	// 游戏界面元素
	constructor() {
		this.game1Click = new ClickIndex()
		GameGlobal.databus.game1Click = this.game1Click; // 全局数据管理，用于管理游戏状态和数据
		console.log("加载分包2")
		this.game1image = GameGlobal.databus.game1image //图片
		this.xiaoxGif = GameGlobal.databus.xiaoxGif //gif
		this.ctx = GameGlobal.databus.ctx //ctx全局
		// // 加载图片后开始启动绘画
		this.loadResources().then(() => {
			this.render()
			GameGlobal.databus.gamethis = this;

		});
	}

	// 更新首页界面
	update() {
		console.log("更新首页界面")
		Home.update(this.ctx) //首页
		Goid.render(this.ctx)
	}
	// 绘制首页界面
	render() {
		console.log("绘制首页")
		console.log("绘制首页",this.game1Click)
		GameGlobal.databus.gameState = 'game1'
		tt.onTouchStart(this.game1Click.handleTouch);
		Home.render(this.ctx) //首页
		Goid.render(this.ctx)
	}
	// 卸载绘图
	notrender() {
		console.log("卸载找不同绘图")
		tt.offTouchStart(this.game1Click.handleTouch);
	}


	// 加载资源
	loadResources() {
		return new Promise((resolve) => {
			Object.keys(this.game1image).forEach(element => {
				this.game1image[element].value = tt.createImage();
				this.game1image[element].value.onload = () => checkLoaded();
				this.game1image[element].value.src = this.game1image[element].url;
			});

			this.xiaoxGif.forEach(element => {
				element.value = tt.createImage();
				element.value.onload = () => checkLoaded();
				element.value.src = element.url;
			});
			let loadedCount = 0;
			const total = Object.keys(this.game1image).length + this.xiaoxGif.length;

			function checkLoaded() {
				if (++loadedCount === total) resolve();
			}
		});
	}
}